package com.zzz.system.controller.sysUser;

import com.zzz.common.core.constants.HttpConstants;
import com.zzz.common.core.controller.BaseController;
import com.zzz.common.core.domain.R;
import com.zzz.common.core.domain.TableDataInfo;
import com.zzz.system.domain.systemUser.dto.LoginDTO;
import com.zzz.system.domain.systemUser.dto.SysUserSaveDTO;
import com.zzz.system.domain.systemUser.SystemUser;
import com.zzz.system.domain.systemUser.vo.LoginUserVo;
import com.zzz.system.domain.systemUser.vo.SysUserVo;
import com.zzz.system.domain.user.dto.UserQueryDTO;
import com.zzz.system.service.sysUser.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @BelongsProject: oj-project
 * @BelongsPackage: com.zzz.system.controller
 * @Author: zhai
 * @CreateTime: 2025-09-21  17:18
 * @Description: 系统用户控制器
 * @Version: 1.0
 */
@Slf4j
@RestController
@Tag(name = "系统用户API模块")
@RequestMapping("/sysUser")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * @description: 管理员登录
     * @author: zhai
     * @date: 2025/10/19 21:21
     * @param: [loginDto]
     * @return: com.zzz.common.core.domain.R<java.lang.String>
     **/
    @PostMapping("/login")
    @Operation(summary = "登录", description = "登录")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    @ApiResponse(responseCode = "3102", description = "用户不存在")
    public R<String> login(@Validated @RequestBody LoginDTO loginDto) {
        log.info("用户登录: {}", loginDto.getUserAccount());
        return sysUserService.login(loginDto.getUserAccount(), loginDto.getPassword());
    }

    /**
     * @description: 添加管理员
     * @author: zhai
     * @date: 2025/10/19 21:22
     * @param: [sysUserSaveDTO]
     * @return: com.zzz.common.core.domain.R<java.lang.Void>
     **/
    @PostMapping("/add")
    @Operation(summary = "添加", description = "根据提供的信息新增管理员用户")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    @ApiResponse(responseCode = "3101", description = "用户已存在")
    public R<Void> add(@Validated @RequestBody SysUserSaveDTO sysUserSaveDTO) {
        log.info("添加用户: {}", sysUserSaveDTO.getNickName());
        return toR(sysUserService.add(sysUserSaveDTO));
    }

    /**
     * @description: 获取当前管理员的身份信息
     * @author: zhai
     * @date: 2025/10/19 21:22
     * @param: [token]
     * @return: com.zzz.common.core.domain.R<com.zzz.system.domain.systemUser.vo.LoginUserVo>
     **/
    @GetMapping("/info")
    public R<LoginUserVo> info(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        log.info("获取用户信息: {}", token);
        return sysUserService.info(token);
    }

    /**
     * @description: 登出
     * @author: zhai
     * @date: 2025/10/19 21:22
     * @param: [token]
     * @return: com.zzz.common.core.domain.R<java.lang.Void>
     **/
    @DeleteMapping("/logout")
    @Operation(summary = "登出", description = "登出")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token) {
        log.info("用户登出: {}", token);
        return sysUserService.logout(token);
    }

    /**
     * @description: 根据id或者是性别获取c端用户的详情信息
     * @author: zhai
     * @date: 2025/10/19 21:22
     * @param: [userId, sex]
     * @return: com.zzz.common.core.domain.R<com.zzz.system.domain.systemUser.vo.SysUserVo>
     **/
    @GetMapping("/detail")
    @Parameters(value = {
            @Parameter(name = "userId", in = ParameterIn.QUERY, description = "用户id"),
            @Parameter(name = "sex", in = ParameterIn.QUERY, description = "性别")
    })
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    @ApiResponse(responseCode = "3102", description = "用户不存在")
    public R<SysUserVo> detail(Long userId, @RequestParam(required = false) String sex) {
        return sysUserService.detail(userId);
    }

    /**
     * @description: 删除用户
     * @author: zhai
     * @date: 2025/10/19 21:23
     * @param: [userId]
     * @return: com.zzz.common.core.domain.R<java.lang.Void>
     **/
    @DeleteMapping("/delete")
    @Operation(summary = "删除", description = "根据用户ID删除用户")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    @ApiResponse(responseCode = "3102", description = "用户不存在")
    public R<Void> delete(Long userId) {
        return toR(sysUserService.delete(userId));
    }

    /**
     * @description: 跟新c端用户信息
     * @author: zhai
     * @date: 2025/10/19 21:23
     * @param: [systemUser]
     * @return: com.zzz.common.core.domain.R<com.zzz.system.domain.systemUser.SystemUser>
     **/
    @PutMapping("/edit")
    @Operation(summary = "更新", description = "更新用户信息")
    @ApiResponse(responseCode = "1000", description = "操作成功")
    @ApiResponse(responseCode = "2000", description = "操作失败")
    @ApiResponse(responseCode = "3102", description = "用户不存在")
    public R<SystemUser> update(@RequestBody SystemUser systemUser) {
        return sysUserService.update(systemUser);
    }
}
